import os
connClanki = pg.connect(dbname='PROBA_POIS_1', host='localhost', user='postgres', passwd='seveda14')
q = "INSERT INTO Kategorija (idkategorija, naziv) VALUES (1,'literatura')"
connClanki.query(q)
for i,j,k in os.walk("C:\\xampp\\htdocs\\python\\publisher\\ClankiSVN\\literatura"):
    parent = i.split("\\")[-1]
    q = "SELECT idkategorija FROM kategorija WHERE naziv='%s'"%parent
    try:
        idParent = connClanki.query(q).getresult()[0][0]
    except:
        continue
    for lit in j:
        try:     
            q = "INSERT INTO Kategorija (naziv, idnadkategorija) VALUES ('%s','%s')"%(lit, idParent)
            connClanki.query(q)
        except:
            pass
    for cl in k:
        if cl.split('.')[-1] == "pdf":
            try:
                leto = int(cl[0:4])
                avtor = cl[4:-4].strip()
                if leto < 1000 or leto > 2008 or len(avtor) <= 0:
                    int("a")
                print avtor
                print i
                q = "INSERT INTO Clanki (naslov, idkategorija, leto, avtor, pot) VALUES ('%s','%s','%s','%s','%s')"%(cl, idParent, leto, avtor, i.replace('\\','/'))
            except:
                q = "INSERT INTO Clanki (naslov, idkategorija, pot) VALUES ('%s','%s','%s')"%(cl, idParent, i.replace('\\','/'))
            connClanki.query(q)